#include <algorithm>
#include <cstdio>

const int N = 1000005;
int t, n;
int fa[N << 1], hash[N << 1];

struct Query {
  int x, y, typ;
  inline bool operator<(const Query &rhs) { return typ > rhs.typ; }
} q[N];

int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

inline void solve() {
  scanf("%d", &n);
  hash[0] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &q[i].x, &q[i].y, &q[i].typ);
    hash[++hash[0]] = q[i].x, hash[++hash[0]] = q[i].y;
  }
  std::sort(hash + 1, hash + hash[0] + 1);
  hash[0] = std::unique(hash + 1, hash + hash[0] + 1) - hash - 1;
  for (int i = 1; i <= hash[0]; ++i) fa[i] = i;
  std::sort(q + 1, q + n + 1);
  int i;
  for (i = 1; i <= n; ++i) {
    if (q[i].typ == 0) break;
    q[i].x = std::lower_bound(hash + 1, hash + hash[0] + 1, q[i].x) - hash;
    q[i].y = std::lower_bound(hash + 1, hash + hash[0] + 1, q[i].y) - hash;
    if (find(q[i].x) != find(q[i].y)) {
      fa[find(q[i].x)] = find(q[i].y);
    }
  }
  for (; i <= n; ++i) {
    q[i].x = std::lower_bound(hash + 1, hash + hash[0] + 1, q[i].x) - hash;
    q[i].y = std::lower_bound(hash + 1, hash + hash[0] + 1, q[i].y) - hash;
    if (find(q[i].x) == find(q[i].y)) return puts("NO"), void();
  }
  puts("YES");
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("程序自动分析.in", "r", stdin);
  freopen("程序自动分析.out", "w", stdout);
#endif
#endif

  scanf("%d", &t);
  while (t--) solve();
  return 0;
}